package DemoLibrarySystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class AccountOperator {
    private ArrayList<Account> accounts=new ArrayList<>();
    Scanner sc=new Scanner(System.in);
    Random r=new Random();

    //展示全部账户
    public void showALLAccount(){
        for (int i = 0; i < accounts.size(); i++) {
            System.out.println("------------------");
            System.out.println("账号："+accounts.get(i).getKeyName());
            System.out.println("密码："+accounts.get(i).getKey());
        }
    }

    //生成4为验证码并返回
    public String proveCode(){
        String s="";
        for (int i = 0; i < 4; i++) {
            int m=r.nextInt(3);
            switch (m){
                case 0:
                    s+=r.nextInt(10);
                    break;
                case 1:
                    s+=(char)(r.nextInt(10)+97);
                    break;
                case 2:
                    s+=(char)(r.nextInt(10)+65);
                    break;
            }
        }
        return s;
    }

    //添加账户
    public void AddAccount(Account a){
        accounts.add(a);
    }
    public void AddAccount(){
        exit:
        while (true) {
            System.out.println("--------注册---------");
            System.out.println("请输入你的用户名：");
            String KeyName = sc.next();
            System.out.println("请输入你的密码：");
            String Key = sc.next();
            System.out.println("请再次输入你的密码：");
            String KeyAgain = sc.next();
            if(!IsSure(KeyName,Key,KeyAgain)) continue exit;
            System.out.println("注册成功~~~");
            Account a = new Account();
            a.setKeyName(KeyName);
            a.setKey(Key);
            accounts.add(a);
            break;
        }
    }

    //判断登录时账号的用户名和密码是否正确
    public boolean IsSure(String KeyName,String Key,String KeyAgain){
        for (int i = 0; i < accounts.size(); i++) {
            if(accounts.get(i).getKeyName().equals(KeyName)){
                System.out.println("账户已存在！");
                return  false;
            }
        }
        if(!Key.equals(KeyAgain)){
            System.out.println("密码错误！");
            return false;
        }
        return true;
    }

    //通过用户名搜索账号并返回该账号在集合中的位置
    public int accountlocal(String KeaName){
        int flag=-1;
        for (int i = 0; i < accounts.size(); i++) {
            if(accounts.get(i).getKeyName().equals(KeaName)) flag=i;
        }
        return flag;
    }

    //执行并检测登录
    public int IsLog(){
        int k=0;
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入你的用户名：");
            String s1=sc.next();
            if(accountlocal(s1)==-1){
                System.out.println("没有该用户！");
                return 0;
            }
            System.out.println("请输入你的密码：");
            String s2=sc.next();
            String ss=proveCode();
            if (!accounts.get(accountlocal(s1)).getKey().equals(s2)){
                System.out.println("用户名或密码错误！");
                k++;
                continue;
            }
            System.out.println("验证码："+ss);
            System.out.println("请输入验证码：");
            String s3=sc.next();
            if(!s3.equalsIgnoreCase(ss)){
                System.out.println("验证码错误错误！");
                k++;
                continue;
            }
            System.out.println("登陆成功！");
            return  1;
        }
        if(k==3){
            System.out.println("你已输错密码3次，禁止输入！！！");
            return 2;
        }
        return 1;
    }

    //进入登录系统
    public int startLog(){
        exit:
        while(true) {
            System.out.println("---====登录图书系统====---");
            System.out.println("注册：L               登录：R");
            String log = sc.next();
            if (log.equals("L") || log.equals("l")) {
                AddAccount();
                continue exit;
            } else if (log.equals("R") || log.equals("r") ) {
                int op=IsLog();
                if(op==1)   return 0;
                if(op==0)   continue exit;
                if(op==2)   System.exit(0);
            }
            if(log.equals("管理员")||log.equals("gly")){
                System.out.println("欢迎管理员进入系统~~~");
                System.out.println("是否查看账号管理：Y           N");
                char q=sc.next().charAt(0);
                if(q=='Y'||q=='y'){
                    showALLAccount();
                    System.out.println("是否进入图书系统：Y          N");
                    char q1=sc.next().charAt(0);
                    if(q1=='Y'||q1=='y')    return 1;
                    else continue exit;
                }
                else return 1;
            }
            else {
                System.out.println("输入不规范！");
                continue exit;
            }
        }
    }
}
